<template>
  <div class="commont-list">
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      :immediate-check='false'
      @load="onLoad"
    >
      <commont-item
        v-for="(item,index) in list"
        :key="index"
        :item="item"
        @reply-show='$emit("reply-show",$event)'
      />
    </van-list>
  </div>
</template>

<script>
import { comments } from '@/api/commont'
import CommontItem from './commont-item'
export default {
  name: 'CommontList',
  components: {
    CommontItem
  },
  props: {
    articleId: {
      type: [Number, String, Object],
      required: true
    },
    list: {
      type: Array, // 对象传值必须为一个函数
      default: () => []
    },
    type: {
      type: String,
      validator (value) {
        return ['a', 'c'].includes(value)
      },
      default: 'a'
    }
  },
  data () {
    return {
      // list: [],
      loading: false,
      finished: false,
      offset: null,
      limit: 20
    }
  },
  created () {
    this.onLoad()
  },
  mounted () {},
  methods: {
    async onLoad () {
      try {
        const { data } = await comments({
          type: this.type,
          source: this.articleId.toString(),
          offset: this.offset,
          limit: this.limit
        })
        console.log(data)
        const { results } = data.data
        this.list.push(...results)
        this.$emit('total-count', data.data)
        this.loading = false
        if (results.length) {
          this.offset = data.data.last_id
        } else {
          this.finished = true
        }
      } catch (err) {
        this.$toast('获取数据失败')
      }
    }
  }
}
</script>

<style scoped lang="less"></style>
